﻿<%@page contentType="text/html; charset=utf-8" language="java" import="cn.ac.cintcm.synonymous.*"  errorPage="" %>
<%@page import="java.util.List, java.util.Date" %>

<% 

String start = request.getParameter("p");
String selectedId = request.getParameter("s");
String w = Util.getUrlParameter(Util.unescape(request.getQueryString()),"w");
String type = request.getParameter("t"); 
String f = Util.getUrlParameter(Util.unescape(request.getQueryString()),"f");  
String method = request.getParameter("m"); 
String edited = request.getParameter("e"); 
String author = request.getRemoteAddr();

String dataSource =  Config.getDataSourceFile();
if (dataSource==null || dataSource.trim().equals("")) {
	response.sendRedirect("upload.html");
}
DataSource ds = CSVDataSource.createDataSource(dataSource);
Dictionary dict = CSVDictionary.getInstance(Config.getDictFile());
Job job = Job.getJobInstance(ds, dict);
EditCtl editCtl = EditCtl.getEditCtlInstance(ds, dict);
Config.reloaded  = false;

Message msg  = null;
int selected = 1;
if (method != null) {
  if (method.equals("sd")) {
  		msg = editCtl.save(); 
  } else if (method.equals("sr")) {
    	msg = editCtl.saveRecord(selectedId, author);
     	if (edited != null && edited.trim().equals(DataSource.INIT_STATE)) {
    	    selectedId = null;
    	}
  }
}
ds = (new FilteredDatasource (ds)).filterState(edited);
DataSourceCtl dctl = new DataSourceCtl((PagedData)ds); 


if (start==null) { 
	start = "1";
}
if (selectedId != null) {
	if (method != null && method.equals("sr")) {
    	selected = ds.getIdIndex(selectedId) ; 
	    if (selected < ds.getLength()) {
	       selected++;
	    }
	    selectedId = ds.getRecordId(selected) + "";
	    start = dctl.getPageStart(selected);

	}
} else {  
	selected =  dctl.getDefaultWorkItem(start);
	selectedId = ds.getRecordId(selected) ;
}

String urls = "index.jsp?e=" + (edited==null?"":edited) + "&";

if (application.getAttribute("count") == null) {
	application.setAttribute("count", new Long(Word.getCounter()));
} 

if (method != null) {
  if (method.equals("gr")) { 
   	  msg =  job.processAll(Config.getResultName());
  }
}

List list = dctl.getData(start);

String navigation = dctl.getNavigation(urls, start);
String primaryName = ds.getPrimaryField();

Record r = job.process(selectedId);
Long counter = (Long) application.getAttribute("count");

if (w != null && !w.trim().equals("")) {
  if ( type != null) {
	  if (type.charAt(0) == EditCtl.ADD_FORMAL) {
	    Word word = new Word();
	    word.setName(w);
	    word.setAlias(false);
	    word.setCreated(new Date());
	    word.setAuthor(author);
	    counter = new Long(counter.longValue() + 1L);
	    word.setId(counter.longValue());
	    msg = editCtl.addWord(word, null);
	  } else if (type.charAt(0) == EditCtl.ADD_INFORMAL) {
	   	  msg = new Message();
        if (f != null) {
            Word word = dict.get(f);
            if (word == null) {
                word = new Word();
                word.setName(f);
                word.setAlias(false);
                word.setCreated(new Date());
                word.setAuthor(author);
                word.setFormalName(f);
	              counter = new Long(counter.longValue() + 1L);
                word.setId(counter.longValue());
                msg = editCtl.addWord(word, null);
            } else {
                msg.setSuccess(true);
            }
            Word ifmw = new Word();
            ifmw.setName(w);
            ifmw.setAlias(true);
            ifmw.setCreated(new Date());
            ifmw.setAuthor(author==null?"":author);
            ifmw.setFormalName(f);
	          counter = new Long(counter.longValue() + 1L);
            ifmw.setId(counter.longValue());
            Message m2 = editCtl.addWord(word, ifmw);
            msg.setSuccess(msg.isSuccess() && m2.isSuccess());
            msg.setMsg(msg.getMsg() + m2.getMsg());
        }
		} else {
		   msg = editCtl.change(w, f, selectedId, r, author, type);
	  }
	r = job.process(selectedId);
	}
}
application.setAttribute ("count", counter);
boolean unsave = editCtl.isUnsaved();

%>

<html>
<head>
  <meta content="text/html; charset=utf-8" http-equiv="content-type">
  <title>正异名修正_数据修正</title>
  <style>
#messageBox{
border-right: 1px solid #000000;
position: absolute;
width: 37px;
height: 130px;
z-index: 1;
background-color: #C0C0C0;
border-style: solid;
border-width: 1px;
display:none;
}
#closeButt{
width: 100%;
height: 10px;
z-index: 1;
cursor: pointer;
left: 0px;
top: 0px;
background-color: #808080;
}
#contents{
width: 100%;
height: auto;
z-index: 2;
}
  </style>
  <script language="javascript">
<!-- Copyright 2006,2007 Bontrager Connection, LLC
// http://bontragerconnection.com/ and http://www.willmaster.com/
// Version: July 28, 2007
var cX = 0; var cY = 0; var rX = 0; var rY = 0;
var selectedWord = " ";
function UpdateCursorPosition(e){ 
	cX = e.pageX; 
	cY = e.pageY;
}
function UpdateCursorPositionDocAll(e){ 
	cX = event.clientX; 
	cY = event.clientY;
}
if(document.all) { 
	document.onmousemove = UpdateCursorPositionDocAll; 
}else {
	document.onmousemove = UpdateCursorPosition; 
}
function AssignPosition(d) {
	if(self.pageYOffset) {
		rX = self.pageXOffset;
		rY = self.pageYOffset;
	} else if(document.documentElement && document.documentElement.scrollTop) {
		rX = document.documentElement.scrollLeft;
		rY = document.documentElement.scrollTop;
	}	else if(document.body) {
		rX = document.body.scrollLeft;
		rY = document.body.scrollTop;
	}
	if(document.all) {
		cX += rX; 
		cY += rY;
	}
	d.style.left = (cX+10) + "px";
	d.style.top = (cY+10) + "px";
}

function HideContent(d) {
	if(d.length < 1) {
		 return; 
	}
	document.getElementById(d).style.display = "none";
}
function ShowContent(d, word) {
	if(d.length < 1) { 
		return; 
	}
	var dd = document.getElementById(d);
	AssignPosition(dd);
	dd.style.display = "block";
	selectedWord  = word;
	document.getElementById("formalName").value = word;
	document.getElementById("newName").value = word;
	document.getElementById("rnewName").value = word;
}
function ReverseContentDisplay(d) {
	if(d.length < 1) { 
		return; 
	}
	var dd = document.getElementById(d);
	AssignPosition(dd);
	if(dd.style.display == "none") {
		 dd.style.display = "block"; 
	} else { 
		 	dd.style.display = "none";
	}
}

function showItems(id) {
 window.onbeforeunload  = null;
 window.location = "<%=urls%>p=<%=start%>&s=" + id;
}

function addFormal() {
	window.onbeforeunload  = null;
	var formal = document.getElementById("formalName"); 
	if (formal.value !=  selectedWord) {
	    window.location = "<%=urls%>p=<%=start%>&s=<%=selectedId%>&t=<%=EditCtl.ADD_INFORMAL%>&w=" +escape(selectedWord) + "&f=" + escape(formal.value);
	} else {
	    window.location = "<%=urls%>p=<%=start%>&s=<%=selectedId%>&t=<%=EditCtl.ADD_FORMAL%>&w=" + escape(selectedWord);
  }
} 


function deleteWord() {
	window.onbeforeunload  = null;
	window.location = "<%=urls%>p=<%=start%>&s=<%=selectedId%>&t=<%=EditCtl.DELETE_WORD%>&w=" + escape(selectedWord);
} 

function deleteDictWord() {
	window.onbeforeunload  = null;
	window.location = "<%=urls%>p=<%=start%>&s=<%=selectedId%>&t=<%=EditCtl.DELETE_DICT_WORD%>&w=" + escape(selectedWord);
} 

function modifyWord() {
	window.onbeforeunload  = null;
	var newName = document.getElementById("newName"); 
	window.location.href = "<%=urls%>p=<%=start%>&s=<%=selectedId%>&t=<%=EditCtl.MODIFY_WORD%>&w=" + escape(selectedWord) + "&f=" + escape(newName.value);;

}

function modifyRWord() {
	window.onbeforeunload  = null;
	var newName = document.getElementById("rnewName"); 
	window.location.href = "<%=urls%>p=<%=start%>&s=<%=selectedId%>&t=<%=EditCtl.MODIFY_WORD%>&w=" + escape(selectedWord) + "&f=" + escape(newName.value);;

}

function save() {
	window.onbeforeunload  = null;
	window.location.href = "<%=urls%>p=<%=start%>&s=<%=selectedId%>&m=sd";
}

function saveRecord () {
	window.onbeforeunload  = null;
	window.location.href = "<%=urls%>p=<%=start%>&s=<%=selectedId%>&m=sr";
} 

function generateResult () {
	window.onbeforeunload  = null;
	window.location.href = "<%=urls%>p=<%=start%>&s=<%=selectedId%>&m=gr";
}

window.onbeforeunload = unloadMess;
function unloadMess(){
	var mess = false;
	if (<%=unsave%>==true) {
	   mess = "有未保存的数据，是否不保存退出？" ;
	   return mess;
	 } else {
	  	window.onbeforeunload  = null;
	 }
}

function navigation(page) {
	window.onbeforeunload  = null; 
	window.location.href = "<%=urls%>p="+page;
}
function filter(condition) {
        window.onbeforeunload  = null;
        window.location.href ="index.jsp?e="+condition;
}
function changeWindow() {
	window.onbeforeunload  = null;
	window.location.href = "words.jsp?n=" + escape(selectedWord) + "&s=<%=selectedId%>" ;
} 

//--> 

  </script>
</head>
<body >
<div id="unrecognized" style="display:none; position:absolute; width: 150px; border-style: solid; background-color: #C0C0C0; padding: 2px;">
<a onClick="addFormal();return true;" href="#">添加为正名</a><input id="formalName" value=""><br>
<a onClick="deleteWord(); return true;" href="#">删除</a><br>
<a onClick="modifyWord(); return true; " href="#">修改</a><input id="newName" value=""><br>
<a onClick="changeWindow();" href="#">切换到词</a><br>
<div onClick="HideContent('unrecognized'); return true;">关闭</div>
</div> 

<div id="recognized" style="display:none; position:absolute; width: 150px; border-style: solid; background-color: #C0C0C0; padding: 2px;">
<a onClick="deleteWord(); return true;" href="#">删除</a><br>
<a onClick="modifyRWord(); return true; " href="#">修改</a><input id="rnewName" value=""><br>
<a onClick="changeWindow();" href="#">切换到词</a><br>
<div onClick="HideContent('recognized'); return true;">关闭</div>
</div>  

<div id="word" style="display:none; position:absolute; width: 150px; border-style: solid; background-color: #C0C0C0; padding: 2px;">
<a onClick="deleteDictWord(); return true;" href="#">删除</a><br>
<a onClick="changeWindow();" href="#">切换到词</a><br>
<div onClick="HideContent('word'); return true;">关闭</div>
</div>   

<table style="text-align: left; width: 100%; height: 33px;"
 border="1" cellpadding="2" cellspacing="2">
  <tbody>
    <tr>
      <td valign="top" width="30%">
      <table style="text-align: left; width: 100%;" border="1"
 cellpadding="2" cellspacing="2">
        <tbody>
          <tr>
            <td><a <%=(edited!=null && edited.equals(DataSource.INIT_STATE))? "" : "onClick=\"filter(" + DataSource.INIT_STATE + ");\" href=\"#\""%>>显示未编辑</a>&nbsp;<a <%=(edited!=null && edited.equals(DataSource.EDIT_STATE))? "" : "onClick=\"filter(" + DataSource.EDIT_STATE + ");\" href=\"#\""%>>显示已编辑</a>&nbsp;<a <%=(edited==null || edited.equals(""))? "" : "onClick=\"filter('');\" href=\"#\""%>>显示全部</a>&nbsp;<a onClick="generateResult();return true;" href="#">生成结果</a>&nbsp;<a onClick="changeWindow();" href="#">切换到词处理</a>&nbsp;<a href="dict.jsp" target="_blank">浏览词典</a>&nbsp;<a href="upload.html">配置</a></td>
          </tr>
          <tr>
            <td>
            <table style="text-align: left; width: 100%;"
 border="1" cellpadding="2" cellspacing="2">
              <tbody>
                <tr>
                  <td colspan="3" rowspan="1"><%=navigation + "<font color=red>编辑了" + editCtl.getRecordEdited()%>条</font></td>
                </tr>

                <tr>
                  <td>标记</td>
                  <td><%=primaryName%></td>
                  <td>选择</td>
                </tr>
<%
 String[] data = null;
  int dataIndex =ds.getFieldIndex(primaryName);
 for (int i = 0; i < list.size(); i++) {
 data = (String[]) list.get(i);
%> 
                <tr>
                  <td style="background-color: #C0C0C0;"><input style="border-width: 2px; border-style: solid; " name="finish" readonly disabled type=checkbox <%=data[ds.getFieldIndex(DataSource.STATE)].equals(DataSource.EDIT_STATE)?"checked":""%> ></td>
                  <td><%=data[dataIndex]%></td>
                  <td><input name="check" type=radio <%=data[ds.getFieldIndex(DataSource.ID)].equals(selectedId)?"checked":""%>  onClick="showItems('<%=data[ds.getFieldIndex(DataSource.ID)]%>');"></td>
                </tr>
<%
}%>

              </tbody>
            </table>
            </td>
          </tr>
        </tbody>
      </table>
      </td> 
<%if (r != null) {%>
      <td valign="top" id="content">
      <table style="text-align: left; width: 100%;" border="1"
 cellpadding="2" cellspacing="2">
        <tbody>
          <tr>
  					<td style="font-weight: bold; color: rgb(255, 0, 0);">
  						<%=(msg==null?"":msg.toString())+ editCtl.getMessage()%>
  					</td>

          </tr>
          <tr>
            <td>
            <table style="text-align: left; width: 100%;"
 border="1" cellpadding="2" cellspacing="2">
              <tbody> 

                <tr>
                  <td>处理数据<%=r.getOriginalLength()%></td>
                  <td>处理结果<%=r.getResultLegnth()%></td>
                  <td>未处理名<%=r.getUnrecognizedLength()%></td>
                  <td>识别正名<%=r.getFormalLength()%></td>
                  <td>识别异名<%=r.getAliasLength()%></td>
                </tr>
                <% 
               	  for (int i=0; i < r.getOriginalLength() ; i++) {
               	   String u = r.getUnrecoginzed()[i] ;
               	   String rw = r.getResult()[i];
               	   String formal = r.getFormal()[i] ;
               	   String informal = r.getInForaml()[i] ;
               	   
               	%>
                <tr>
                  <td><%=r.getOriginal()[i]%></td>
                  <td><%=(rw==null||rw.equals(""))?"":"<a onClick=\"ShowContent('recognized','" + rw + "'); return true;\" href=\"#\">" + rw + "</a>"%></td>
                  <td><%=(u==null||u.equals(""))?"":"<a onClick=\"ShowContent('unrecognized','" + u + "'); return true;\" href=\"#\">" + u + "</a>"%></td>
                  <td><%=(formal==null||formal.equals(""))?"":"<a onClick=\"ShowContent('word','" + formal + "'); return true;\" href=\"#\">" + formal + "</a>"%></td>
                  <td><%=(informal==null||informal.equals(""))?"":"<a onClick=\"ShowContent('word','" + informal + "'); return true;\" href=\"#\">" + informal + "</a>"%></td>
                </tr>
               	<% 
               	  }
               	%>

              </tbody>
            </table>
            </td>
          </tr>
          <tr>
           	<td> 
            <table style="text-align: left; width: 100%;"
 border="1" cellpadding="2" cellspacing="2">
              <tbody>
               	<% 
               	   List displayMeta = ds.getDisplayFields(); 
               	   String[] selectedData = ds.getRecord(selectedId);
               	   for (int i=0; i < displayMeta.size(); i++) {
               	        String display = (String) displayMeta.get(i);
               	%>
                <tr>
                  <td width="20%"><%=display%></td>
                  <td><%=selectedData[ds.getFieldIndex(display)]%></td>
                </tr>
								<% 
								   }
								%>
              </tbody>
            </table>
           	</td>
          </tr>
        </tbody>
      </table>

      </td> 
<%}%>
    </tr>
  </tbody>
</table>
<br>

</body>
</html>
